using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkillObject_SwordSpin : SkillObject_Sword
{
    private float maxDistance;
    private float attackPerInterval;
    private float maxDuration;
    private float attackTimer;
    protected override void Update()
    {
        HandlerSwordComeBack();
        HandlerSwordToMaxDistance();
        HnadlerSwordDamage();
    }
    public override void SetupSword(Skill_SwordThrow skill_SwordThrow, Vector2 initDirection)
    {
        base.SetupSword(skill_SwordThrow, initDirection);
        maxDistance = swordThrowManager.maxDistance;
        maxDuration = swordThrowManager.maxDuration;
        attackPerInterval = swordThrowManager.attackPerInterval;
        animator?.SetBool("Spin", true);
        Invoke(nameof(SetBack), maxDuration);
    }
    //处理剑掷出到最大距离
    private void HandlerSwordToMaxDistance()
    {
        float distance = Vector2.Distance(transform.position, playerTrans.position);
        //当剑掷出到最远距离时
        if (distance > maxDistance && rb.simulated == true)
        {
            //停止对该刚体的物理模拟
            rb.simulated = false;
            //类比碰撞，当剑达到掷出的最大距离时才能触发回收机制
            isCollide = true;
        }
    }
    private void HnadlerSwordDamage()
    {
        attackTimer -= Time.deltaTime;
        if (attackTimer <= 0)
        {
            attackTimer = attackPerInterval;
            DamageEnemiesInRedius(transform, 1f);
        }
    }
    protected override void OnTriggerEnter2D(Collider2D collision)
    {
        //停止对该刚体的物理模拟
        rb.simulated = false;
        //当剑碰撞时时才能触发回收机制
        isCollide = true;
    }
    

}
